layui.use(['form', 'layedit', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer;

    //自定义验证规则
    form.verify({
        username: function (value) {
            if (value.length < 1) {
                return '账号不能为空！';
            }
        }
        , pass: [
            /^[\S]{5,16}$/
            , '密码必须5到16位，且不能出现空格'
        ]
        , content: function (value) {
            console.log(value)
        }
    });


    //监听提交
    form.on('submit(formButton)', function (data) {
        // layer.alert(JSON.stringify(data.field), {
        //     title: '最终的提交信息'
        // });
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        var imgCode = $("input[name='imgCode']").val();
        var md5_password = hex_md5(password);

        $.ajax({
            url: "/login",
            type: "post",
            dataType: "json",
            beforeSend: function(){
                layer.msg('loading···', {
                    icon: 16
                    ,shade: 0.01
                    ,time:1000
                });
            },
            data: {
                username: username,
                password: md5_password,
                imgCode: imgCode
            },
            async: true,
            success: function (result) {
                if (result.code) {
                    //layer.alert(result.msg, "title");
                    //location.href = "http://" + window.location.host + "/main.html";
                    layer.msg(result.msg, {icon: 1,time:2000}, function(){
                        location.href = "http://" + window.location.host + "/main.html";
                    });
                } else {
                    // layer.alert(result.msg);
                    var ico = 5;
                    if(result.icon!=null){
                        ico = result.icon
                    }
                    getVerify();
                    layer.msg(result.msg, {icon: ico,time:3000}, function(){
                        //getVerify();
                    });
                }
            },
            error: function () {
                layer.alert("系统繁忙,请稍后");
            }
        });
        //拦截提交，不使用from表单方式提交
        return false;
    });


});

//获取验证码
function getVerify() {
    $("#imgVerify").attr("src", '/getVerify?' + Math.random());//jquery方式
}

function getEmailCode() {
    $("#getEmailCode").html("10秒")
    
}